# InputController examples

from peripherals import TimedInputController
from peripherals import InputInterface

class Forwarder(TimedInputController):
	def __init__(self, inputInterface):
		TimedInputController.__init__(self, inputInterface, 40)
		self.__x = 0
		self.__phi = 0
		
	def DoWork(self):
		interface = self._inputInterface
		self.__x = 1 + self.__x
		self.__phi = 1 + self.__phi
		interface.SetPositionInstrument(self.__x, 0,0)
		interface.SetOrientationInstrument(0, 0,self.__phi)

import wx		
		
class SimpleJoystickController(TimedInputController):
	def __init__(self, inputInterface):
		self.__js = wx.Joystick()
		TimedInputController.__init__(self, inputInterface, 30)

	def DoWork(self):
		#poll the joystick
		x,y = self.__js.GetPosition()
		b = self.__js.GetButtonState()

		#transform the value's to something a bit more workable
		x = (x - self.__js.GetXMax()/2.0) / (4.0 * self.__js.GetXMax())
		y = (y - self.__js.GetYMax()/2.0) / (4.0 * self.__js.GetYMax())
		z = 0
		
		#handle the button presses
		if b == 1:
			z = -0.5
		if b == 2:
			z = 0.5
		
		#get information about the position of the instrument
		xo, yo, zo = self._inputInterface.GetPositionInstrument()
		
		#map joystick values to the HeaDSET world
		xn = xo + x
		yn =  yo - y
		zn = zo + z
		
		#insert the values into HeaDSET
		self._inputInterface.SetPositionInstrument(xn, yn, zn)		
	